package com.genius.workflow.util;

import java.sql.Connection;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import javax.servlet.http.HttpSession;

import com.alibaba.fastjson.JSONObject;
import com.genius.adminmanager.other.entity.ResponseJson;

/**
 * 获取编号的实体类
 * @author Administrator
 *
 */
public class GetNo {
	//获取要货单号
	//根据门店号和日期获取数据库中的最大flowno,编号规则为Y+门店编号+日期+4位序号
	public static String getflowno(String shopcode,String table){
		StringBuilder flowno = null;
        Connection conn = null;
        try {
        	if(table == null){
        		table = "order_list";
        	}
        	SimpleDateFormat sdf = new SimpleDateFormat("yyMMdd");
    		String date = sdf.format(Calendar.getInstance().getTime());
    		flowno = new StringBuilder("Y"+shopcode+date);
        	//打开连接
        	conn = DBUtils.getConnect();
        	String sql = "select LPAD(RIGHT(max(flowno),4)+1,4,0)xuhao from "+table+" where"
        			+ " flowno >= 'Y"+shopcode+date+"0001' and flowno <= 'Y"+shopcode+date+"9999'";
//        	String sql = "select LPAD(RIGHT(max(flowno),4)+1,4,0)xuhao from order_list a where "
//        			+ "flowno >= DATE_FORMAT(CURDATE(),'Y"+shopcode+"%y%m%d0001') and flowno <= DATE_FORMAT(CURDATE(),'Y"+shopcode+"%y%m%d9999')";
        	ResultSet rs = DBUtils.query(conn, sql);
        	//存在，取出序号+1;
        	String xuhao = null;
        	if (rs.next()) {
        		xuhao = rs.getString("xuhao");
			}
        	if(xuhao == null){
        		xuhao = "0001";
        	}
        	rs.close();
        	flowno.append(xuhao);
            
        } catch (Exception e) {
           e.printStackTrace();
        } finally {
        	DBUtils.closeDBResources(conn);
		}
        return flowno.toString();
	};


	public static String getNotice(){
		StringBuilder flowno = null;
		Connection conn = null;
		try {
			String table = "notice_info";
			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
			String date = sdf.format(Calendar.getInstance().getTime());
			flowno = new StringBuilder("G"+date);
			//打开连接
			conn = DBUtils.getConnect();
			String sql = "select LPAD(RIGHT(max(id),4)+1,4,0)xuhao from "+table+" where"
					+ " id >= 'G"+date+"0001' and id <= 'G"+date+"9999'";
//        	String sql = "select LPAD(RIGHT(max(flowno),4)+1,4,0)xuhao from order_list a where "
//        			+ "flowno >= DATE_FORMAT(CURDATE(),'Y"+shopcode+"%y%m%d0001') and flowno <= DATE_FORMAT(CURDATE(),'Y"+shopcode+"%y%m%d9999')";
			ResultSet rs = DBUtils.query(conn, sql);
			//存在，取出序号+1;
			String xuhao = null;
			if (rs.next()) {
				xuhao = rs.getString("xuhao");
			}
			if(xuhao == null){
				xuhao = "0001";
			}
			rs.close();
			flowno.append(xuhao);

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBUtils.closeDBResources(conn);
		}
		return flowno.toString();
	};
	
	
	public static JSONObject getnotice(JSONObject response) {
		StringBuilder flowno = null;
        Connection conn = null;
        String  table = "notice_send_info";
        try {
        	SimpleDateFormat sdf = new SimpleDateFormat("yyMMdd");
    		String date = sdf.format(Calendar.getInstance().getTime());
    		flowno = new StringBuilder(date);
        	//打开连接
        	conn = DBUtils.getConnect();
        	String sql = "select LPAD(RIGHT(max(no),4)+1,4,0)xuhao from "+table+" where"
        			+ " no >='"+ date+"0001' and no <='"+date+"9999'";
        	ResultSet rs = DBUtils.query(conn, sql);
        	//存在，取出序号+1;
        	String xuhao = null;
        	if (rs.next()) {
        		xuhao = rs.getString("xuhao");
			}
        	if(xuhao == null){
        		xuhao = "0001";
        	}
        	rs.close();
        	flowno.append(xuhao);
        	response.put("no",flowno );
            response.put(ResponseJson.status, ResponseJson.status_Success);
        } catch (Exception e) {
        	  response.put(ResponseJson.status, ResponseJson.status_Failed);
           e.printStackTrace();
        } finally {
        	DBUtils.closeDBResources(conn);
		}
        
        return response;
		

	}
}
